<?php
/**
 * 单页类
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Singlepage extends MY_Controller {
    
    private $model='singlepage';
    private $table='singlepage';
    
    function __construct()
    {
        parent::__construct();
        $this->check_manage_session();//判断用户是否登录
    }
    
    public function index()
    {
        $this->page();
    }
    
    //列表方法
    public function page()
    {
		$data['list'] = $this->admin_model->get_list($this->table);
        $data['sidelist'] = $this->admin_model->get_list("channel","","","","sort ASC,id DESC");
        $this->load->view($this -> config->item('system_manage_path').'singlepage_list',$data);
    }
    
    public function add()
    {
        $data['show'] = $this->admin_model->get_select_max($this->table);
        $data['sidelist'] = $this->admin_model->get_list('channel','','','','sort ASC,id DESC');
        $this->load->view($this -> config->item('system_manage_path').'singlepage_add',$data);
    }
    
    public function edit($id){
        $data['show'] = $this->admin_model->get_show($this->table,$id);
        $data['sidelist'] = $this->admin_model->get_list('channel','','','','sort ASC,id DESC');
        $this->load->view($this -> config->item('system_manage_path').'singlepage_edit',$data);
    }
    
    public function save($action,$id=NULL){
        $post=$this->input->post();
        $data=unset_array($post,'lcomeurl'); //过滤无效表单项
    
        if($action=='add' or $action=='edit'){
            if(empty($post['title'])){
                $this->error('请输入单页名称',2);
            }
        }
    
        $lcomeurl=get_comurl($this->input->post('lcomeurl'));
    
        if($action=='add'){
            $action_text="添加";
		    $data['createtime'] = time();
            $result=$this->admin_model->add($this->table,$data);
        }elseif($action=='edit'){
            $action_text="修改";
            if(empty($id)){
                $this->error('参数错误',2);
            }
            $result=$this->admin_model->edit($this->table,$data,$id);
        }elseif($action=='del'){
            $action_text="删除";
            if(empty($id)){
                $this->error('参数错误',2);
            }
            $result=$this->admin_model->del($this->table,$id);
        }
    
        if($result){
            $lcomeurl=str_replace("welcome", "channel", $lcomeurl);
            $this->success($action_text.'成功',2,$lcomeurl);
        }else{
            $this->error($action_text.'失败',2);
        }
    }
}